import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
from datetime import datetime

class LikeShopTest():
    def __init__(self) -> None:
        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    
    def test_login(self):
        print("开始登录...")
        self.browser.get('http://likeshop.abc/admin/account/login.html')
        sleep(2)
        #定位用户名输入框输入用户名
        self.browser.find_element(By.NAME,'account').send_keys('admin')
        #定位密码输入框输入密码
        self.browser.find_element(By.NAME,'password').send_keys('123456')
        #定位登录按钮并点击登录
        self.browser.find_element(By.ID,'login').click()
        sleep(2)
        print("登录完成")
        
         
    #商品列表查询 
    def test_product_list(self):
        print("开始商品列表查询...")
        #定位商品标签并点击商品
        self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[2]/div/ul/li[1]/a/cite').click()
        sleep(2)
            
        #退出子框架
        self.browser.switch_to.default_content()
        print("商品列表查询完成")
        
        
    #商品添加
    def test_product_add(self):
        print("开始商品添加...")
        
        # 定位商品标签并点击商品
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        print("点击商品菜单")
        sleep(2)
        
        # 定位子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # 切换到子框架
        self.browser.switch_to.frame(iframe)
        print("切换到主框架")
        
        # 定位发布商品按钮并点击发布商品
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        print("点击发布商品按钮")
        sleep(2)
        
        # 定位商品发布子框架
        iframe_add = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        # 切换到商品发布子框架
        self.browser.switch_to.frame(iframe_add)
        print("切换到商品发布框架")
        
        now = datetime.now().strftime('%Y%m%d%H%M%S')

        # 定位商品名称输入框输入商品名称
        self.browser.find_element(By.NAME, 'name').send_keys('测试商品' + now)
        print("输入商品名称")

        sleep(2)
        
        # 选择商品分类 - 第一级
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        print("选择第一级分类")
        sleep(1)
        
        # 选择商品分类 - 第二级
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        print("选择第二级分类")
        sleep(1)
        
        # 选择商品分类 - 第三级
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        print("选择第三级分类")
        sleep(1)
        
        # 定位商品主图的添加图片组件并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        print("点击添加商品主图")
        sleep(2)
        
        # 上传商品主图
        self.test_upload_img('D:\\Software\\project\\1.jpg', '/html/body/div[6]/div[2]/iframe')
        
        # 切换回商品发布框架
        self.browser.switch_to.frame(iframe_add)
        
        # 定位商品轮播图的添加图片组件并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        print("点击添加商品轮播图")
        sleep(2)
        
        # 上传商品轮播图
        self.test_upload_img('D:\\Software\\project\\1.jpg', '//*[@id="layui-layer-iframe3"]')
        
        # 切换回商品发布框架
        self.browser.switch_to.frame(iframe_add)
        
        # 切换到价格库存标签页
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()
        print("切换到价格库存标签页")
        sleep(3)

        # 定位市场价(元)输入框输入市场价(元)
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('6')
        sleep(1)
        
        # 定位价格(元)输入框输入价格(元)
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('6')
        sleep(1)
        
        # 定位成本价(元)输入框输入成本价(元)
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('6')
        sleep(1)
        
        # 定位库存信息框输入库存
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('1000')
        sleep(1)
        print("完成价格库存设置")

        # 切换到销售批量标签页
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[4]').click()
        print("切换到销售批量标签页")
        sleep(3)
        
        # 选择配送方式的快递发货
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[4]/div[6]/div/div[1]/i').click()
        sleep(1)
        print("选择配送方式")

        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        
        # 点击保存
        self.browser.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        print("点击保存按钮")
        sleep(2)
        
        self.browser.switch_to.parent_frame()
        
        
    #图片上传
    def test_upload_img(self, img_path, xpath_path):
        
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        
        # 定位图片上传子框架
        # 切换到图片上传子框架
        upload_img_iframe_1 = self.browser.find_element(By.XPATH, xpath_path)
        self.browser.switch_to.frame(upload_img_iframe_1)

        # 定位图片输入框输入图片路径
        self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[1]/input').send_keys(img_path)
        sleep(4)
        # 选择图片
        self.browser.find_element(By.XPATH, '//*[@id="lists"]/li[1]/div/img').click()
        sleep(2)
        # 点击使用图片
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()
        sleep(2)
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        
        
    #商品上架   
    def test_product_shangjia(self):
        
        # 定位商品标签并点击商品
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        
        # 定位子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # 切换到子框架
        self.browser.switch_to.frame(iframe)
        
        # 切换到仓库中商品
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[2]').click()
        sleep(2)
        
        # 接收列表
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        if len(tr_list) == 0:
            print('仓库中暂无商品')
            return
            
        # 点击第一个商品的上架按钮
        tr_list[0].find_element(By.XPATH, './td/div/a[2]').click()
        sleep(1)
        
        # 确认上架
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        sleep(2)
        print("商品上架完成")
        
        
      #退出登陆  
    def test_logout(self):
        print("开始退出登录...")

        # 确保回到最外层
        self.browser.switch_to.default_content()
        sleep(2)

        # 现在再找用户名菜单
        dt = self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/ul[2]/li[3]/a/cite')
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(1)

        # 点击退出
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        print("退出登录完成")
        
if __name__ == '__main__':
        lk = LikeShopTest()
        lk.test_login()
        lk.test_product_list()
        lk.test_product_add()
        #lk.test_upload_img()
        lk.test_product_shangjia()
        lk.test_logout()
  

